package com.ahocorasick.config;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.exception.NacosException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Properties;

public class NacosConfigProcessor implements ConfigProcessor {

    private static final Logger logger = LoggerFactory.getLogger(NacosConfigProcessor.class);

    @Autowired
    private NacosConfig nacosConfig;

    @Override
    public void process() {
        try {
            // 初始化 Nacos NamingService
            Properties properties = new Properties();
            properties.put("serverAddr", nacosConfig.getServerAddr());
            NamingService namingService = NacosFactory.createNamingService(properties);

            // 创建一个服务实例
            Instance instance = new Instance();
            instance.setIp(nacosConfig.getIp());
            instance.setPort(nacosConfig.getPort());
            // 设置其他实例属性

            // 注册服务实例
            namingService.registerInstance(nacosConfig.getServiceName(), instance);

            logger.info("服务注册成功: {}", nacosConfig.getServiceName());
        } catch (NacosException e) {
            logger.error("Nacos 服务注册失败", e);
        }
    }
}
